package com.definity.toolkit.ui.widget;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;

import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.text.JTextComponent;

import com.definity.toolkit.i18n.I18n;
import com.definity.toolkit.i18n.I18nFactory;

public abstract class InternalFrame extends JInternalFrame {

	private static final long serialVersionUID = 4525577674621806361L;

	private Container container;
	private JPanel content;

	private boolean destroyed;

	protected WidgetBuilder b = new WidgetBuilder();
	
	protected InternalFrame(String keyTitle, Object... args) {
		setTitle(String.format(i18n().tl(keyTitle), args));

		container = getContentPane();
		container.setLayout(new BorderLayout());

		content = createContentPanel();
		container.add(content);

		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(680, 430);
		setMaximizable(true);
		setIconifiable(true);
		setClosable(true);
	}

	public Icon getFrameIcon() {
		return null;
	}
	
	@Override
	public void setTitle(String title) {
		super.setTitle(title);
	}
	
	@Override
	public final void setVisible(boolean aFlag) {
		super.setVisible(aFlag);
//		
//		EventDispatcher dispatcher = EventDispatcher.getInstance();
//		if (aFlag) {
//			dispatcher.fire(new OpenFrameEvent(this));
//		} else {
//			dispatcher.fire(new CloseFrameEvent(this));
//		}
	}
	
	public boolean isDestroyed() {
		return destroyed;
	}

	public void init() {
		destroyed = false;
		
		loadDataInformation();
	}

	public boolean isSingleInstance() {
		return true;
	}

	public final void loadData() {
		loadDataInformation();
	}
	
	protected void loadDataInformation() {

	}

	protected I18n i18n() {
		return I18nFactory.getApplication();
	}

	protected abstract JPanel createContentPanel();

	protected void clearFields() {
		clearFields(content);
	}

	protected void clearFields(Container container) {
		Component[] components = container.getComponents();
		for (Component component : components) {
			if (component instanceof JTextComponent) {
				((JTextComponent) component).setText("");
			} else if (component instanceof JComboBox) {
				((JComboBox) component).setSelectedIndex(0);
			} else if (component instanceof JCheckBox) {
				((JCheckBox) component).setSelected(false);
			} else if (component instanceof JRadioButton) {
				((JRadioButton) component).setSelected(false);
			} else if (component instanceof Container) {
				clearFields((Container) component);
			}
		}
	}

	public void close() {
		if (getDefaultCloseOperation() == HIDE_ON_CLOSE) {
			setVisible(false);
			return;
		}
		
		if (!isDestroyed())
			destroy();

		content = null;

		super.dispose();
	}

	public void destroy() {
		destroyed = true;
		
		close();
	}

	@Override
	public void dispose() {
		close();
	}

}
